<?php include_once( "_db.php" ); //Database ?>
<?php include_once( "funcdef.php" ); //Functions Definition ?>
<?php include_once( "header.php" ); //Header ?>
<?php include_once( "footer.php" ); //Footer ?>
<?php
//include page header
header_template("Slider", $_SERVER['PHP_SELF'], "icon-play-circle");

//db OBJECT invoked!
$db = new DATABASE();
?>
      <div class="row">
        <div class="span6">
<?php
if ($_SERVER['REQUEST_METHOD'] == 'GET') {
  //Submit button for Create
  $button_submit_name  = "create-slider";
  $button_submit_value = "Create";
  //Hidden slider id
  $hidden_slider_id = "";
  //Slider for Create
  $resPostg = array();
  if (isset($_GET["action"]) && isset($_GET["slid_id"])) {
    if ($_GET["action"] == "edit") {
      //Slider for Edit
      //construct SELECT query
      $conds = array();
      $conds[] = array(0); //$param_col;
      $conds[] = array(0); //$param_op;
      $conds[] = array($_GET["slid_id"]); //$param_key;
      $query = $db->db_select("slider", NULL, $conds);
      //execute SELECT query
      $result = $db->database->query($query);
      //result SELECT query
      $resSlid = $result->fetchAll(SQLITE_ASSOC);
      //Submit button for Edit
      $button_submit_name  = "edit-slider";
      $button_submit_value = "Edit";
      //Hidden slider id
      $hidden_slider_id = "<input type='hidden' name='slider_id' value='".$resSlid[0]['slider_id']."' />";
    }
  }
//Slider Form
?>
          <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" enctype="application/x-www-form-urlencoded" class="form-horizontal">
            <?php echo $hidden_slider_id."\n"; ?>
            <fieldset>
              <div class="control-group">
                <label class="control-label" for="slider_name">Name</label>
                <div class="controls">
                  <input type="text" class="span3" id="slider_name" name="slider_name" value="<?php echo $resSlid[0]['slider_name']; ?>">
                </div>
              </div>
              <div class="control-group">
                <label class="control-label" for="slider_label">Label</label>
                <div class="controls">
                  <textarea rows="3" class="span3" id="slider_label" name="slider_label"><?php echo $resSlid[0]['slider_label']; ?></textarea>
                  <!--
                  <input type="text" class="input-xlarge" id="slider_label" name="slider_label" value="<?php echo $resSlid[0]['slider_label']; ?>">
                  -->
                </div>
              </div>
              <div class="control-group">
                <label class="control-label" for="slider_show">Show</label>
                <div class="controls">
                  <select class="span3" id="slider_show" name="slider_show">
<?php
  if ($resSlid[0]['slider_show'] == "yes") {
    $slider_show_yes = " selected='selected'";
    $slider_show_no = "";
  } elseif ($resSlid[0]['slider_show'] == "no") {
    $slider_show_yes = "";
    $slider_show_no = " selected='selected'";
  } else {
    //show default
    $slider_show_yes = "";
    $slider_show_no = " selected='selected'";
  }
?>
                    <option value="no"<?php echo $slider_show_no; ?>>no</option>
                    <option value="yes"<?php echo $slider_show_yes; ?>>yes</option>
                  </select>
                  <!--
                  <input type="text" class="input-xlarge" id="slider_show" name="slider_show" value="<?php echo $resSlid[0]['slider_show']; ?>">
                  -->
                </div>
              </div>
              <div class="form-actions">
                <input type="submit" name="<?php echo $button_submit_name; ?>" value="<?php echo $button_submit_value; ?>" class="btn btn-info" />
              </div>
            </fieldset>
          </form>
<?php
} elseif ($_SERVER['REQUEST_METHOD'] == 'POST') {
  //Create Slider
  if ($_POST['create-slider'] == "Create") {
    //Form field check NG
    if ($_POST["slider_name"] == "") {
      printAlert("error", "Oh snap!", "Slider Name field is empty.", $_SERVER['PHP_SELF']);
    } elseif ($_POST["slider_label"] == "") {
      printAlert("error", "Oh snap!", "Slider Label field is empty.", $_SERVER['PHP_SELF']);
    } elseif ($_POST["slider_show"] == "") {
      printAlert("error", "Oh snap!", "Slider Show field is empty.", $_SERVER['PHP_SELF']);
    } else {//Form field check OK
      //construct INSERT query
      $params = array();
      $params[] = array(1, 2, 3); //$param_col;
      $params[] = array($_POST["slider_name"], $_POST["slider_label"], $_POST["slider_show"]); //$param_key;
      $query = $db->db_insert("slider", $params);
      //execute INSERT query
      $result = $db->database->query($query);
      //Add slider NG
      if ($result == FALSE) {
        printAlert("error", "Oh snap!", "Cannot create Slider '".$_POST["slider_name"]."'.", $_SERVER['PHP_SELF']);
      } else {//Add slider OK
        //INSERT SUCCEED!!!
        printAlert("success", "Well done!", "Slider '".$_POST["slider_name"]."' successfully created.", $_SERVER['PHP_SELF']);
      }
    }
  }
  //Edit Slider
  if ($_POST['edit-slider'] == "Edit") {
  echo "<pre>";
  var_dump($_POST);
  echo"</pre>";
    //Form field check NG
    if ($_POST["slider_name"] == "") {
      printAlert("error", "Oh snap!", "Slider Name field is empty.", $_SERVER['PHP_SELF']);
    } elseif ($_POST["slider_label"] == "") {
      printAlert("error", "Oh snap!", "Slider Label field is empty.", $_SERVER['PHP_SELF']);
    } elseif ($_POST["slider_show"] == "") {
      printAlert("error", "Oh snap!", "Slider Show field is empty.", $_SERVER['PHP_SELF']);
    } else {//Form field check OK
      //construct UPDATE query
      $params = array();
      $params[] = array(1, 2, 3); //$param_col;
      $params[] = array($_POST["slider_name"], $_POST["slider_label"], $_POST["slider_show"]); //$param_key;
      $conds = array();
      $conds[] = array(0); //$param_col;
      $conds[] = array(0); //$param_op;
      $conds[] = array($_POST["slider_id"]); //$param_key;
      $query = $db->db_update("slider", $params, $conds);
      //execute UPDATE query
      $result = $db->database->query($query);
      //Edit slider NG
      if ($result == FALSE) {
        printAlert("error", "Oh snap!", "Cannot edit Slider '".$_POST["slider_name"]."'.", $_SERVER['PHP_SELF']);
      } else {//Edit slider OK
        //UPDATE SUCCEED!!!
        printAlert("success", "Well done!", "Slider '".$_POST["slider_name"]."' successfully edited.", $_SERVER['PHP_SELF']);
      }
    }
  }
} else {
  $error_msg = "This script only works with GET and POST requests.";
  printAlert("error", "Oh snap!", $error_msg, $_SERVER['PHP_SELF']);
}

//Delete Slider
if (isset($_GET["action"]) && isset($_GET["slid_id"])) {
  if ($_GET["action"] == "delete") {
    //CHECK FOR bill2slider
    $conds = array();
    $conds[] = array(0); //$param_col;
    $conds[] = array(0); //$param_op;
    $conds[] = array($_GET["slid_id"]); //$param_key;
    $query = $db->db_select("slider", NULL, $conds);
    //execute SELECT query
    $result = $db->database->query($query);
    //result SELECT query
    $resSlid = $result->fetchAll(SQLITE_ASSOC);
    //construct DELETE query
    $query = $db->db_delete("slider", $conds);
    //execute DELETE query
    $result = $db->database->query($query);
    //delete slider NG
    if ($result == FALSE) {
      printAlert("error", "Oh snap!", "Cannot delete ".$resSlid[0]['slider_country_name'], $_SERVER['PHP_SELF']);
    } else {//delete slider OK
      //DELETE SUCCEED!!!
      printAlert("success", "Well done!", $resSlid[0]['slider_country_name']." successfully deleted", $_SERVER['PHP_SELF']);
      //construct DELETE params
      //$conds = array();
      //$conds[] = array(2); //$param_col;
      //$conds[] = array(0); //$param_op;
      //$conds[] = array($_GET["slid_id"]); //$param_key;
      //construct DELETE query
      //$query = $db->db_delete("bill2slider", $conds);
      //execute DELETE query
      //$result = $db->database->query($query);
      // if ($result == FALSE) {
        // printAlert("error", "Oh snap!", "Cannot delete ".$resSlid[0]['slider_country_name'], $_SERVER['PHP_SELF']);
      // } else {//delete slider OK
        //DELETE SUCCEED!!!
        // printAlert("success", "Well done!", $resSlid[0]['slider_country_name']." successfully deleted", $_SERVER['PHP_SELF']);
      // }
    }
  }
}
?>
          <table class="table table-condensed table-striped">
            <thead>
              <tr>
                <th>Name</th>
                <th>Label</th>
                <th>Show</th>
                <th>&nbsp;</th>
              </tr>
            </thead>
            <tbody>
<?php
//construct SELECT query
$query = $db->db_select("slider", NULL, NULL);
//execute SELECT query
$result = $db->database->query($query." ORDER BY slider_id DESC");
//result SELECT query
$sliders = $result->fetchAll(SQLITE_ASSOC);
//List slider(s)
//Slider(s) existence OK
if (!empty($sliders)) {
  //loop trough all slider(s)
  foreach ($sliders as $slid) {
    $slider_edit_href = $_SERVER['PHP_SELF']."?action=edit&slid_id=".$slid['slider_id'];
    $slider_delete_href = $_SERVER['PHP_SELF']."?action=delete&slid_id=".$slid['slider_id'];
?>
              <tr>
                <td><?php echo $slid['slider_name']; ?></td>
                <td><?php echo $slid['slider_label']; ?></td>
                <td><?php echo $slid['slider_show']; ?></td>
                <td>
                  <a class="btn btn-success btn-mini" href="<?php echo $slider_edit_href; ?>">Edit</a>
                  <a class="btn btn-danger btn-mini" href="<?php echo $slider_delete_href; ?>">Delete</a>
                </td>
              </tr>
<?php
  }
}
?>
            </tbody>
          </table>
        </div>
      </div>
<?php
//include page footer
footer_template();
?>
